--[[
  点击时，请求网络，导致页面严重卡顿时，采用下面的方法
]]

-- 页面跳转 - 找一个固定色，判断
function UtilFnLoopPageColor(fn, cy)
  local cx = 10
  cy = cy or 140
  -- 自定义 - 要循环的背景坐标
  local preColor = getColor(cx, cy)
  local nextColor = nil

  --
  local rdx,rdy = 896,518
  local rdxPre = getColor(rdx, rdy)
  local rdxNext = nil

  -- 导航栏
  local navBgx,navBgy = 16,16
  local navBgCo = getColor(navBgx,navBgy)

  -- 屏幕底部中间
  local scrrenBottomMidx,scrrenBottomMidy = 280,270
  local scrrenBottomMidCo = getColor(scrrenBottomMidx,scrrenBottomMidy)

  local loginBtnx,loginBtny = 430,476
  local loginBtnCo = getColor(loginBtnx,loginBtny)

  local dhTopRightx,dhTopRighty = 608,228
  local dhTopRightCo = getColor(dhTopRightx,dhTopRighty)

  local ctn = fn()
  if ctn == 1 then
    mSleep(600)
    local whileNum = 0
    local whileDo = true
    while whileDo do
      local load = GameAlertLoading()
      if load then mSleep(1000) end
      if whileNum > 300 then
        ctn = 0
        break
      end
      nextColor = getColor(cx, cy)
      if nextColor ~= preColor then
        RxLog('__ UtilFnLoopPageColor __ UtilAutoPointColor 000_start_000')
        UtilAutoPointColor(cx, cy)
        RxLog('__ UtilFnLoopPageColor __ UtilAutoPointColor 000_end_000')
        break
      end
      rdxNext = getColor(rdx, rdy)
      if rdxNext ~= rdxPre then
        RxLog('__ UtilFnLoopPageColor 222 __ UtilAutoPointColor 000_start_000')
        UtilAutoPointColor(rdx, rdy)
        RxLog('__ UtilFnLoopPageColor 222 __ UtilAutoPointColor 000_end_000')
        break
      end
      local cuNavBgCo = getColor(navBgx,navBgy)
      if cuNavBgCo ~= navBgCo then break end

      -- local cuScrrenBottomMidCo = getColor(scrrenBottomMidx,scrrenBottomMidy)
      -- if cuScrrenBottomMidCo ~= scrrenBottomMidCo then break end

      local cuLoginBtnCo = getColor(loginBtnx,loginBtny)
      if cuLoginBtnCo ~= loginBtnCo then break end

      local cuDhTopRightCo = getColor(dhTopRightx,dhTopRighty)
      if cuDhTopRightCo ~= dhTopRightCo then break end

      RxLog('...wait-fn-'..tostring(whileNum))
      whileNum = whileNum + 1
      mSleep(10)
    end
  end
  return ctn
end

-- 方法后，查看某个固定色 判断方法执行页面变了
function UtilFnTouchBgColor(fn, x, y, isLoop)
  local ctn = 0
  x = x or 92
  y = y or 16
  isLoop = isLoop or 0
  local bgColor = getColor(x, y)

  function loopBg()
    mSleep(1000)
    local whileNum = 1
    while true do
      if whileNum > 100 then
        RxTouch(905,502)
        mSleep(1000)
        UtilAutoPointColor()
      end
      local co = getColor(x, y)
      if co ~= bgColor then
        RxTouchRandom()
        mSleep(1500)
      else
        break
      end

      whileNum = whileNum + 1
    end
  end

  function callFcb()
    local c = fn()
    RxLog('___UtilFnTouchBgColor -> callFcb -=> c='..tostring(c))
    if c == 1 then
      ctn = 1
      loopBg()
    end
    return c
  end

  RxLog('___UtilFnTouchBgColor isLoop='..tostring(isLoop))
  if isLoop == 1 then
    local n = 1
    while true do
      if n > 20 then break end
      local c = callFcb()
      RxLog('___UtilFnTouchBgColor while-do c='..tostring(c))
      if c == 0 then
        break
      end
    end
  else
    callFcb()
  end

  return ctn
end

function UtilFnLoopTouchBgColor(fn, x, y)
  x = x or 92
  y = y or 16
  return UtilFnTouchBgColor(fn, x, y, 1)
end

function UtilAutoPointColor(x, y, diff)
  x = x or 1
  y = y or 1
  diff = diff or 0
  local touchColor = nil
  local whileNum = 0
  local topColor = getColor(1, 1)

  local whileMaxNum = 220

  while true do
    if whileNum > whileMaxNum then
      break
    end
    GameAlertLoading()
    touchColor = getColor(x, y)
    mSleep(10)
    local toColor = getColor(x, y)
    RxLog('...wait-auto-point..__wait 1='..tostring(touchColor)..' 2='..tostring(toColor)..'  __diff='..tostring(diff)..' x='..tostring(x)..' y='..tostring(y))
    if diff >= 1 then
      if touchColor ~= toColor then
        break
      end
    else
      if touchColor == toColor then
        break
      elseif whileNum > 80 then
        local nextTopColor = getColor(1,1)
        if topColor == nextTopColor then
          break
        end
      end
    end
    mSleep(1)
    whileNum = whileNum + 1
  end
end

function UtilAutoDiffPointColor(x,y)
  x = x or 1
  y = y or 1
  UtilAutoPointColor(x, y, 1)
end

-- 字符串转table: "a,b,c" => {"a", "b", "c"}
function UtilStringSplit(str, splitChar)
  splitChar = splitChar or ','
  mSleep(1)
  local ussTab = {}
  local ussSize_t = 1
  -- RxLog('UtilStringSplit str='..tostring(str))
  if not str or RxString:len(str) < 1 then
    return {''}
  end

  while true do
    local pos = string.find(str, splitChar)
    local t = string.len(str)
    if pos == nil then
      local sr = string.sub(str, 1, t)
      ussTab[ussSize_t] = tostring(sr)
      break
    end

    local sr = string.sub(str, 1, pos - 1)
    ussTab[ussSize_t] = sr
    str = string.sub(str, pos + 1, t)
    ussSize_t = ussSize_t + 1
  end
  return ussTab
end

-- table 转成 字符串: {"a", "b", "c"} => "a,b,c"
function UtilTableConcat(utable, splitChar)
  local text = ''
  splitChar = splitChar or ' '
  if utable == nil then
    return text
  end
  for k,v in pairs(utable) do
    mSleep(10)
    RxLog('UtilTableConcat k='..tostring(k)..' v='..tostring(v))
    if text ~= '' then
      text = text .. splitChar
    end
    text = text .. tostring(v)
  end
  return text
end

-- 本地图片 缓存地址 -- 图片有 日期字符串 作为图片名，获取的地址是唯一的
-- -- 常用于 存储 新图片名
function UtilImageNamePath(name, dir)
  mSleep(9)
  name = name or 'unknow'
  dir = dir or ''
  local t = os.time()
  local uname = name .. '__'..t..'.png'
  if dir and dir ~= '' and dir ~= ' ' then
    dir = '/res/'..tostring(dir).. '/'
  else
    dir = '/res/cache/'
  end
  local mpath = userPath() .. dir
  -- 图片路径 不存在，创建目录，只能创建1级目录
  RxDhFile:AutoCreate(mpath)
  local fullPath = mpath .. uname
  return fullPath, uname
end


-- 本地图片 固定图片名
-- -- 常用于 脚本 自带的图片
function UtilImageFixPath(name, dir)
  mSleep(9)
  name = name or 'unknow'
  dir = dir or ''
  local uname = name ..'.png'
  if dir ~= '' or dir ~= ' ' then
    dir = '/res/'..dir.. '/'
  else
    dir = '/res/cache/'
  end
  local mpath = userPath() .. dir
  -- 图片路径 不存在，创建目录，只能创建1级目录
  RxDhFile:AutoCreate(mpath)
  local fullPath = mpath .. uname
  return fullPath, uname
end

-- 根据Node.js打包后，判断环境，获取本地图片地址
-- 用于 打包脚本 没有开源，这里就不要用了
function UtilImageEnvPath(name)
  name = name or 'unknow'
  local res = '/res/'

  -- 联调环境
  if developEnv < 1 or developEnv > 9 then
    res = res .. 'assets/'
  end

  local fullUrl = '' .. userPath() .. res .. name
  local isExist = RxDhFile:file_exist(fullUrl)
  RxLog('UtilImageEnvPath fullUrl=' .. fullUrl..'--isExist='..tostring(isExist))
  return fullUrl
end

-- 判断 方法执行后的返回值，是否为1
-- -- 常用于 多级页面返回，确保每次返回都是正确的
function UtilFnZeroRun(ctn, fn)
  if ctn == 0 then
    ctn = fn()
    if ctn == 1 then
      mSleep(10)
      UtilAutoPointColor()
    end
  end
  return ctn
end

function UtilPageJugeToast(ctn, log)
  if ctn == 0 then
    RxToast(log)
  end
end

-- 按钮 某个[位置]颜色是一致的就点击
function UtilYellowButton(pointTable, colorLimitTab,isNegate)
  isNegate = isNegate or false -- 范围内，判断取反，结果是真理
  local ctn,x,y = UtilPointLimit(pointTable, colorLimitTab,isNegate)
  if ctn == 1 then
    RxTouch(x,y)
    RxLog('___---___UtilYellowButton RxTouch(x, y)'..tostring(x)..'--y='..tostring(y))
    mSleep(1000)
    UtilAutoPointColor(x, y)
  end
  return ctn,x,y
end

-- 某个位置， 是否为 某个颜色 (常用于 固定位置固定颜色)
function UtilPointLimit(pointTable, colorLimitTab,isNegate)
  isNegate = isNegate or false -- 范围内，判断取反，结果是真理
  colorLimitTab = colorLimitTab or {0xffd000, 0xffffff}
  local x,y = pointTable[1], pointTable[2]
  local min,max = colorLimitTab[1], colorLimitTab[2]

  local ctn = 0
  local co = getColor(x,y)
  RxLog('___---UtilPointLimit co='..tostring(co)..' min='..tostring(min)..' max='..tostring(max)..'--x='..tostring(x)..'--y='..tostring(y)..'---isNegate='..tostring(isNegate))
  if isNegate then
    if co < min or co > max then
      ctn = 1
    end
  elseif co >= min and co <= max then
    ctn = 1
  end
  RxLog('___---UtilPointLimit 结果 ctn='..tostring(ctn))
  return ctn,x,y
end
